package com.codeoss.framework.view.renderer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.codeoss.framework.context.YunContext;
import com.codeoss.framework.expression.Expression;
import com.codeoss.framework.expression.IScriptService;
import com.codeoss.framework.expression.IScriptServiceStore;
import com.codeoss.framework.util.StringUtils;
import com.codeoss.framework.view.component.Component;
import com.codeoss.framework.view.component.Label;

@Service
public class LabelRenderer extends CommonComponentRenderer {
	@Autowired
	private IScriptServiceStore ScriptServiceStore;
	protected void startRenderView(YunContext yunContext, Component component, ViewSerializeMap viewSerializeMap) {
		super.startRenderView(yunContext, component, viewSerializeMap);
		Label label = (Label) component;
		String dataField=label.getDataField();
		if(!StringUtils.isEmpty(dataField)) {
			viewSerializeMap.put("dataField", dataField);
		}
		Expression labelExpression = label.getLabelExpression();
		if (labelExpression != null) {
			IScriptService scriptService=ScriptServiceStore.getScriptService(labelExpression);
			Object value = scriptService.eval(labelExpression.getScript(), yunContext.getVarValueMap());
			viewSerializeMap.put("label", value);
		}
	}
	
	@Override
	protected PropSerializeMap startRenderProp(YunContext yunContext, Component component, ViewPropMap viewPropMap) {
		PropSerializeMap propSerializeMap = super.startRenderProp(yunContext, component, viewPropMap);
		Label label = (Label) component;
		Expression labelExpression = label.getLabelExpression();
		if (labelExpression != null) {
			IScriptService scriptService=ScriptServiceStore.getScriptService(labelExpression);
			Object value = scriptService.eval(labelExpression.getScript(), yunContext.getVarValueMap());
			propSerializeMap.put("label", value);
		}
		
		return propSerializeMap;
	}
}
